package com.bestcem.xm.txtanls.service.dto.analysis;

import com.bestcem.xm.txtanls.entity.mongo.AnalysisProject;
import com.bestcem.xm.txtanls.entity.pojo.ProjectDO;
import com.bestcem.xm.txtanls.entity.pojo.ProjectFieldDO;
import com.bestcem.xm.txtanls.enums.AnalysisStatusEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 文本分析项目对外展示DTO
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/5/11-11:23
 */
@ApiModel("文本分析项目信息")
@ToString
@NoArgsConstructor
@Getter
@Setter
public class AnalysisProjectDTO {
    @JsonProperty(value = "id")
    @ApiModelProperty(value = "数据记录唯一标识")
    private String id;

    /**
     * 创建用户id
     */
    @JsonProperty(value = "user_id")
    @ApiModelProperty(value = "创建用户id")
    private String userId;

    /**
     * 创建用户名
     */
    @JsonProperty(value = "creator")
    @ApiModelProperty(value = "创建用户名")
    private String creator;

    /**
     * 项目名称
     */
    @JsonProperty(value = "name")
    @ApiModelProperty(value = "项目名称")
    private String name;

    /**
     * 组织id
     */
    @JsonProperty(value = "org_id")
    @ApiModelProperty(value = "组织id")
    private String orgId;

    /**
     * 文本分析项目状态
     *
     * @see com.bestcem.xm.txtanls.enums.AnalysisStatusEnum
     */
    @JsonProperty(value = "status")
    @ApiModelProperty(value = "文本分析项目状态;  从 0到6 分别是: 未开始, 训练中, 已失效, 已删除, 失败, 已创建, 已完成")
    private Integer status;

    /**
     * 1.2新增，项目状态中文（包含训练中（1）、等待中（0和5）、训练完成（6）三种状态）
     */
    @JsonProperty(value = "status_name")
    @ApiModelProperty(value = "项目状态（包含训练中（1）、等待中（0和5）、训练完成（6）三种状态）")
    private String statusName;
    /**
     * 问卷id
     */
    @JsonProperty(value = "survey_id")
    @ApiModelProperty(value = "问卷id")
    private String surveyId;

    /**
     * 数据源表id
     */
    @JsonProperty(value = "table_id")
    @ApiModelProperty(value = "数据源表id")
    private String tableId;

    /**
     * 数据源表名
     */
    @JsonProperty(value = "table_name")
    @ApiModelProperty(value = "数据源表名")
    private String tableName;

    /**
     * 训练字段信息
     */
    @JsonProperty(value = "textfieldList")
    @ApiModelProperty(value = "训练字段信息")
    private List<AnalysisProject.TextField> textFieldList;

    /**
     * 时间字段id
     */
    @JsonProperty(value = "timefield_id")
    @ApiModelProperty(value = "时间字段id")
    private String timeFieldId;

    /**
     * 时间字段名字
     */
    @JsonProperty(value = "timefield_name")
    @ApiModelProperty(value = "时间字段名字")
    private String timeFieldName;

    /**
     * 项目训练状态信息
     */
    @JsonProperty(value = "train_msg")
    @ApiModelProperty(value = "项目训练状态信息")
    private TrainMsg trainMsg;

    /**
     * 项目训练状态
     *
     * @see com.bestcem.xm.txtanls.enums.TrainStatusEnum
     */
    @JsonProperty(value = "train_msg.status")
    @ApiModelProperty(value = "项目训练状态;  从 0到1 分别是: 0-已完成, 1-未完成")
    private Integer trainStatus;

    /**
     * 项目训练剩余时间 (格式: X时X分)
     */
    @JsonProperty(value = "train_msg.train_remain_time")
    @ApiModelProperty(value = "项目训练剩余时间 (格式: X时X分)")
    private String trainRemainTime;

    /**
     * 数据记录标记
     *
     * @see com.bestcem.xm.txtanls.enums.RecordFlagStatusEnum
     */
    @JsonProperty(value = "record_flag")
    @ApiModelProperty(value = "数据记录标记")
    private Integer recordFlag;

    @JsonProperty(value = "created_dt")
    @ApiModelProperty(value = "数据记录创建时间")
    private Date createdDate;
// 与mongo saas的返回参数保持一致
//    @JsonProperty(value = "createdID")
//    @ApiModelProperty(value = "数据记录创建id")
//    private String createdId;

    @JsonProperty(value = "updated_dt")
    @ApiModelProperty(value = "数据记录更新时间")
    private Date updatedDate;

    @JsonProperty(value = "updatedID")
    @ApiModelProperty(value = "数据记录更新id")
    private String updatedId;

    @JsonProperty(value = "sample")
    @ApiModelProperty(value = "是否是示例项目")
    private Boolean sample;

    /**
     * 模型id
     */
    @JsonInclude
    @JsonProperty(value = "model_id")
    private String modelId;

    @ApiModel("训练字段信息")
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class TrainMsg implements Serializable {
        private static final long serialVersionUID = 2808726620840986656L;

        /**
         * 项目训练状态
         *
         * @see com.bestcem.xm.txtanls.enums.TrainStatusEnum
         */
        @JsonProperty(value = "status")
        @ApiModelProperty(value = "项目训练状态;  从 0到2 分别是: 0-已完成, 1-未完成,2-等待中")
        private Integer trainStatus;

        /**
         * 项目训练剩余时间 (格式: X时X分)
         */
        @JsonProperty(value = "train_remain_time")
        @ApiModelProperty(value = "项目训练剩余时间 (格式: X时X分)")
        private String trainRemainTime;
    }


    /**
     * 文本分析项目 实体转DTO
     * gyliu 训练状态和进度修改 commit_hash: 3666344816be626c484c9a032691b605ae290561
     *
     * @param entity 实体
     * @return DTO for entity conversion
     */
    public static AnalysisProjectDTO analysisEntityToDTO(ProjectDO entity) {
        AnalysisProjectDTO dto = new AnalysisProjectDTO();
        dto.setId(entity.getId());
        dto.setUserId(entity.getCreator());
        dto.setCreator(entity.getCreator());
        dto.setName(entity.getName());
        dto.setOrgId(entity.getOrgId());
        dto.setStatus(entity.getStatus());
        dto.setSurveyId(entity.getSurveyId());
        dto.setTableId(entity.getTableId());
        dto.setTableName(entity.getTableName());
        List<AnalysisProject.TextField> textFieldList = new ArrayList<>();
        List<ProjectFieldDO> projectFieldDOList = entity.getTextFieldList();
        for (ProjectFieldDO projectFieldDO : projectFieldDOList) {
            textFieldList.add(new AnalysisProject.TextField(projectFieldDO.getId(), projectFieldDO.getValue()));
        }
        dto.setTextFieldList(textFieldList);
        dto.setTimeFieldId(entity.getTimeFieldId());
        dto.setTimeFieldName(entity.getTimeFieldName());

        // 训练状态和剩余时间类型修改为字符串
        dto.setTrainStatus(entity.getTrainStatus());
        int count = Objects.isNull(entity.getTrainProgress()) || entity.getTrainProgress() < 0 ? 0 : entity.getTrainProgress();
        // 阿里云接口一秒只能训练20条文本, 剩余数量转换为分钟
        int minuteValue = count / 20 / 60;
        String trainProgress = minuteValue / 60 + "时" + minuteValue % 60 + "分";
        dto.setTrainRemainTime(trainProgress);
        dto.setTrainMsg(new TrainMsg(dto.getTrainStatus(), dto.getTrainRemainTime()));

        // 返回给前端展示的状态中文,1.2新增
        dto.setStatusName(AnalysisStatusEnum.getStatusName(entity.getStatus(), entity.getTrainStatus(), entity.getTrainProgress()));
        dto.setSample(entity.getSample());
        dto.setModelId(entity.getModelId());
        dto.setRecordFlag(entity.getRecordFlag());
        dto.setCreatedDate(entity.getCreatedDate());
//        dto.setCreatedId(entity.getCreatedId());
        dto.setUpdatedDate(entity.getUpdatedDate());
        dto.setUpdatedId(entity.getUpdatedId());
        return dto;
    }
}
